# -*-coding:utf-8-*-
__author__ = 'libb@anchnet.com'
"""
参考链接：https://mp.weixin.qq.com/s?__biz=MzU2MzgyODA4OA==&mid=2247484566&idx=2&sn=72c643ef461950609913aea720c7ca30&chksm=fc550081cb22899773bbeaba64913898e02867b849c7d8e5d9bcdc9af6313b2a693705f48c84&mpshare=1&scene=1&srcid=&sharer_sharetime=1576649606908&sharer_shareid=acd2d89a65813b197bc61322ac0ea3f4&key=d30bd6efb358fbe7eb41740f89ab5e153505e65da96cebc00b404d1891757f241da13a98d39ec270d23ba08906bee9631ad9ffc1a141226e760e34e3b115c5d089cf450d24519a5f7865df3aa4c7395d&ascene=1&uin=ODc3Mzc5NjIx&devicetype=Windows+10&version=62070158&lang=zh_CN&exportkey=AaXdf950eu52Xfha5JTb0q0%3D&pass_ticket=aKNJeklgzVPHtnrtPrU7kkX0wE6a848xGShu9g%2B74Rl99MbFNY%2Fio%2BRPVx%2FOYXb%2F

pymongo模块的使用
1.连接数据库
1.1 连接方式：使用host+port
client1 = MongoClient(host="localhost", port=27017)
1.2  连接方式：使用mongodb url
url = 'mongodb://localhost/'
client2 = MongoClient(url)

1.3 MongoDB概念和术语
database  == database    数据库
table     == collection  数据库表/集合
row       == document    数据记录行/文档
column    == field       数据字段/域
index     == index       索引 

2.数据库的增删该查

"""
from pymongo import MongoClient

url = 'mongodb://root:ProcessManagePlatform2022@11.149.109.96:27017/admin'
client2 = MongoClient(url)
print(client2)

collection1 = client2['mongo']['腾讯会议']
# # collection 相当于SQL中的table
collection2 = client2['mongo']['腾讯会议']
print(collection2.find_one())
# # 增加，插入
# ret = collection1.insert_one({
#     "task_info_list": [
#         {
#             "taskType": 0,
#             "id": "12345",
#             "name": "stroy1",
#             "status": "测试中",
#             "bug_total_num": 1,
#             "bum_resolved_num": 2,
#             "case_total_num": 1,
#             "case_executed_num": 2,
#             "duration": 4,
#             "test_start_time": "2022-01-01",
#             "test_end_time": "2022-01-01",
#             "last_modified_time": "2022-01-01",
#             "created_time": "2022-01-01"
#         }
#     ],
#     "test_owner": "urftian1"
# })
# print(ret)
# collection1.insert_many([{'people_num': 112222}, {'city_num': 222}])
# collection2.insert_one({'people_num': 112222, 'city_num': 222, 'location': 'east'})
#
# # 默认查询集合中的第一条
# print(collection2.find_one())
# # 条件查询
# print(collection2.find_one({'people_num': 112222, 'city_num': 222, 'location': 'east'}))
# # 查询集合中所有数据
# print(collection2.find({}, {"_id": 0, "city_num": 1}))
# # 限制返回条数，limit（n）当n大于原本的数据数量是，返回原数据量
# print(collection2.find({}).count())
# print(collection2.find({}).count())
#
# # 更新 update_one(), update_many()
# collection2.update_one({'people_num': 112222}, {"$set": {"people_num": "11111"}})
# print(collection2.find_one({"people_num": "11111"}))
# collection2.update_many({'city_num': 222}, {"$set": {"city_num": 22222}})
# print(collection2.find_one({"city_num": 22222}))
#
#
# # 删除delete_one和delete_many()
# collection2.delete_one({"city_num": 22222})
# print(collection2.find_one({"city_num": 22222}))
